<template>
  <el-card>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>新闻管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-button type="primary" style="margin:15px 0" @click="addDialogVisible=true">添加分类</el-button>
    <el-row>
      <el-col :span="12">
        <el-table :data="classList" stripe border style="width: 100%">
          <el-table-column prop="name" label="名称" width="180"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="showEditClass(scope.row.classId)"
              ></el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="deleteClassById(scope.row.classId)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <!-- 添加分类用到的对话框 -->
    <el-dialog title="添加分类" :visible.sync="addDialogVisible" width="50%" @close="addDialogClose">
      <el-form :model="addForm" ref="addForm" label-width="100px">
        <el-form-item label="名称">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>

        <el-form-item label="图标名称" prop="icon">
          <el-input v-model="addForm.icon"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addClass">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑分类用到的对话框 -->
    <el-dialog title="编辑分类" :visible.sync="editDialogVisible" width="50%" @close="editDialogClose">
      <el-form :model="editForm" ref="editForm" label-width="100px">
        <el-form-item label="名称">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>

        <el-form-item label="图标名称" prop="icon">
          <el-input v-model="editForm.icon"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editClass">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>
<script>
export default {
  // eslint-disable-next-line space-before-function-paren
  data() {
    return {
      // 分类列表
      classList: [],
      // 添加分类的对话框的状态
      addDialogVisible: false,
      // 添加分类表单
      addForm: {
        name: '',
        icon: ''
      },
      editDialogVisible: false,
      editForm: {
        name: '',
        icon: ''
      }
    }
  },
  // eslint-disable-next-line space-before-function-paren
  created() {
    this.getClassList()
  },
  methods: {
    // eslint-disable-next-line space-before-function-paren
    async getClassList() {
      const { data: res } = await this.$http.get('api/class')
      this.classList = res.Table
    },
    // 删除分类按钮
    // eslint-disable-next-line space-before-function-paren
    async deleteClassById(id) {
      const removeClassById = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      // console.log(removeUserById)
      // console.log(id)
      // 取消了删除
      if (removeClassById !== 'confirm') {
        return this.$message.info('取消了删除')
      }
      console.log(id)
      const { data: res } = await this.$http.post('api/class?value=' + id)
      console.log(res)

      if (res.status !== 200) return this.$message.error('删除用户失败')
      this.$message.success('删除用户成功')
      this.getClassList()
    },
    // 点击编辑按钮
    // eslint-disable-next-line space-before-function-paren
    async showEditClass(id) {
      this.editDialogVisible = true
      // console.log(id)

      // 发送请求获取数据
      const { data: res } = await this.$http.get('/api/class/' + id)
      this.editForm.classId = id
      this.editForm.name = res.Table[0].name
      this.editForm.icon = res.Table[0].icon

      console.log(res)
    },
    // eslint-disable-next-line space-before-function-paren
    // eslint-disable-next-line space-before-function-paren
    addDialogClose() {
      // 重置表单
      this.$refs.addForm.resetFields()
    },
    // 点击了添加用户对话框中的确定键
    // eslint-disable-next-line space-before-function-paren
    async addClass() {
      this.addDialogVisible = false
      // console.log(this.addForm)
      // 将数据发送到后台
      var params = new URLSearchParams()
      params.append('name', this.addForm.name)
      params.append('icon', this.addForm.icon)

      const { data: res } = await this.$http({
        method: 'post',
        url: 'api/class',
        data: params,
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
      })
      console.log(res)
      if (res.status !== 200) return this.$message.error('添加分类失败')
      this.$message.success('添加分类成功')
      this.getClassList()
    },
    // eslint-disable-next-line space-before-function-paren
    editDialogClose() {
      this.$refs.editForm.resetFields()
    },
    // 点击了编辑对话框中的确定
    // eslint-disable-next-line space-before-function-paren
    async editClass() {
      const data = await this.$http.get('/api/class/', {
        params: {
          id: this.editForm.classId,
          name: this.editForm.name,
          icon: this.editForm.icon
        }
      })
      console.log(data)
      this.$message.success('修改成功')
      this.editDialogVisible = false
      this.getClassList()
    }
  }
}
</script>
<style lang="less" scoped>
</style>
